﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace MisClient
{
    public partial class FrmSalesEnquiries : BaseForm
    {
        private IBLL.IDepart departBLL = MisCLL.DAOFactory.CreateInstance().GetRemoteDepart();
        private IBLL.ISacf sacfBLL = MisCLL.DAOFactory.CreateInstance().GetRemoteSacf();
        private IBLL.IGoods goodsBLL = MisCLL.DAOFactory.CreateInstance().GetRemoteGoods();
        private IBLL.IType typeTjBLL = MisCLL.DAOFactory.CreateInstance().GetRemoteType();
        private IBLL.ICustomer cusBLL = MisCLL.DAOFactory.CreateInstance().GetRemoteCustomer();
        private IBLL.ISupply supplyBLL = MisCLL.DAOFactory.CreateInstance().GetRemoteSupply();

        private WinPrint winPrint = new WinPrint();

        private int dataGridView = 0;
        private int printType = 1;

        public FrmSalesEnquiries()
        {
            InitializeComponent();
        }

        private void FrmSalesEnquiries_Load(object sender, EventArgs e)
        {
            this.expireStarDate.Format = DateTimePickerFormat.Custom;
            this.expireStarDate.CustomFormat = " ";
            this.expireEndDate.Format = DateTimePickerFormat.Custom;
            this.expireEndDate.CustomFormat = " ";
            this.txtDepCode.Text = accountUser.DeptCode;
            this.openStarDate.Value = DateTime.Now.Date.AddDays(-1);
            this.openStarDate.Focus();

            //业务员初始化
            this.linker.ValueMember = "CUS_ID";
            this.linker.DisplayMember = "LINKER";
            this.linker.DataSource = cusBLL.FindBusinessMan(); 
            this.linker.Text = "";

            //业务员1初始化
            this.linker1.ValueMember = "CUS_ID";
            this.linker1.DisplayMember = "F2";
            this.linker1.DataSource = cusBLL.FindBusinessMan1();
            this.linker1.Text = "";
        }

        private void txtDepCode_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                if (string.IsNullOrEmpty(this.txtDepCode.Text.Trim()))
                {
                    MessageBox.Show("部门代码不能为空！", "信息窗", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    this.txtDepCode.Focus();
                    return;
                }
                MisModel.Depart depart = departBLL.GetById(this.txtDepCode.Text.Trim());
                if (depart == null)
                {
                    MessageBox.Show("此部门无权操作！", "信息窗", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    this.txtDepCode.Focus();
                    return;
                }
                this.txtBoxDep.Text = depart.DepName;
                this.txtGysCode.Focus();
            }
        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(this.txtDepCode.Text.Trim()))
            {
                MessageBox.Show("部门代码不能为空！", "信息窗", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                this.txtDepCode.Focus();
                return;
            }
            MisModel.Depart depart = departBLL.GetById(this.txtDepCode.Text.Trim());
            if (depart == null)
            {
                MessageBox.Show("此部门无权操作！", "信息窗", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                this.txtDepCode.Focus();
                return;
            }
            Dictionary<string, object> dictionary = new Dictionary<string, object>();
            StringBuilder sb = new StringBuilder();
            this.dataGridView1.Rows.Clear();

            if (!"1".Equals(this.txtDepCode.Text.Trim()))
            {
                sb.Append(" and m.dep_id=@depId");
                dictionary.Add("@depId", this.txtDepCode.Text.Trim());
            }
            sb.Append(" and m.date>=@startDate and m.date<=@endDate");
            dictionary.Add("@startDate", this.openStarDate.Value.ToShortDateString());
            DateTime time = DateTime.Parse(this.openEndDate.Value.ToShortDateString());
            time = time.Date.AddDays(1);
            dictionary.Add("@endDate", time.ToShortDateString());
            if (!string.IsNullOrEmpty(this.expireStarDate.Text.ToString().Trim()))
            {
                sb.Append(" and m.d_kp>=@gqStartDate");
                dictionary.Add("@gqStartDate", this.expireStarDate.Value.ToShortDateString());
            }
            if (!string.IsNullOrEmpty(this.expireEndDate.Text.ToString().Trim()))
            {
                sb.Append(" and m.d_kp<=@gqEndDate");
                time = DateTime.Parse(this.expireEndDate.Value.ToShortDateString());
                time = time.Date.AddDays(1);
                dictionary.Add("@gqEndDate", time.ToShortDateString());
            }
            if (!string.IsNullOrEmpty(this.txtID.Text.Trim()))
            {
                sb.Append(" and m.id=@id");
                dictionary.Add("@id", this.txtID.Text.Trim());
            }
            if (!string.IsNullOrEmpty(this.txtShopCode.Text.Trim()))
            {
                sb.Append(" and m.shop_id=@shopId");
                dictionary.Add("@shopId", this.txtShopCode.Text.Trim());
            }
            if (!string.IsNullOrEmpty(this.txtNo.Text.Trim()))
            {
                sb.Append(" and m.no=@no");
                dictionary.Add("@no", this.txtNo.Text.Trim());
            }
            if (!string.IsNullOrEmpty(this.txtCusCode.Text.Trim()))
            {
                sb.Append(" and m.cus_id=@cusId");
                dictionary.Add("@cusId", this.txtCusCode.Text.Trim());
            }
            if (!string.IsNullOrEmpty(this.txtGysCode.Text.Trim()))
            {
                sb.Append(" and g.supp_id=@suppId");
                dictionary.Add("@suppId", this.txtGysCode.Text.Trim());
            }
            if (!string.IsNullOrEmpty(this.txtCusDepCode.Text.Trim()))
            {
                sb.Append(" and m.cus_dep=@cusDep");
                dictionary.Add("@cusDep", this.txtCusDepCode.Text.Trim());
            }
            if (!string.IsNullOrEmpty(this.txtOper.Text.Trim()))
            {
                sb.Append(" and m.oper=@oper");
                dictionary.Add("@oper", this.txtOper.Text.Trim());
            }
            if (!string.IsNullOrEmpty(this.txtTax.Text.Trim()))
            {
                sb.Append(" and m.tax=@tax");
                dictionary.Add("@tax", this.txtTax.Text.Trim());
            }
            if (!string.IsNullOrEmpty(this.txtCusShopCode.Text.Trim()))
            {
                sb.Append(" and m.cus_shop=@cusShop");
                dictionary.Add("@cusShop", this.txtCusShopCode.Text.Trim());
            }
            if (!string.IsNullOrEmpty(this.txtBei.Text.Trim()))
            {
                sb.Append(" and m.bei=@bei");
                dictionary.Add("@bei", this.txtBei.Text.Trim());
            }
            if (!string.IsNullOrEmpty(this.txtInvoiceNum.Text.Trim())) 
            {
                sb.Append(" and m.no_cus=@noCus");
                dictionary.Add("@noCus", this.txtInvoiceNum.Text.Trim());
            }
            if (!string.IsNullOrEmpty(this.txtFKID.Text.Trim()))
            {
                sb.Append(" and m.type_js=@typeJs");
                dictionary.Add("@typeJs", this.txtFKID.Text.Trim()); 
            }
            if (!string.IsNullOrEmpty(this.txtType1.Text.Trim()))
            {
                sb.Append(" and g.type1 like @type1");
                dictionary.Add("@type1", this.txtType1.Text.Trim() + "%");
            }
            if (!string.IsNullOrEmpty(this.txtType2.Text.Trim()))
            {
                sb.Append(" and g.type2=@type2");
                dictionary.Add("@type2", this.txtType2.Text.Trim());
            }
            if (!string.IsNullOrEmpty(this.txtType3.Text.Trim()))
            {
                sb.Append(" and g.type3 like @type3");
                dictionary.Add("@type3", this.txtType3.Text.Trim() + "%");
            }
            string tjStr = this.comboBox1.SelectedItem.ToString();
            if ("已开单".Equals(tjStr))
                sb.Append(" and m.f1='1'");
            else if ("已开单未确认提货".Equals(tjStr))
                sb.Append(" and m.f1='1' and m.f2=''");
            else if ("已确认未提货".Equals(tjStr))
                sb.Append(" and m.f2='0'");
            else if ("未提货,已开发票".Equals(tjStr))
                sb.Append(" and m.f2!='1' and m.f3='1'");
            else if ("已确认提货".Equals(tjStr))
                sb.Append(" and m.f2='1'");
            else if ("已提货,已开发票".Equals(tjStr))
                sb.Append(" and m.f2='1' and m.f3='1'");
            else if ("已提货,未开发票".Equals(tjStr))
                sb.Append(" and m.f2='1' and m.f3!='1'");
            else if ("已开发票".Equals(tjStr))
                sb.Append(" and m.f3='1'");
            else if ("已开发票,未到款".Equals(tjStr))
                sb.Append(" and m.f3='1' and m.f4!='1'");
            else if ("已开发票,已到款".Equals(tjStr))
                sb.Append(" and m.f3='1' and m.f4='1'");
            else if ("已结束".Equals(tjStr))
                sb.Append(" and m.f8='1'");
            tjStr = this.comboBox2.SelectedItem.ToString();
            if ("普通特价".Equals(tjStr))
                sb.Append(" and m.f6='1'");
            else if ("发票高开".Equals(tjStr))
                sb.Append(" and m.f6='2'");
            else if ("非特价".Equals(tjStr))
                sb.Append(" and m.f6=''");
            tjStr = this.comboBox3.SelectedItem.ToString();
            if ("小于0(退货)".Equals(tjStr))
                sb.Append(" and m.sl<0");
            else if ("大于0".Equals(tjStr))
                sb.Append(" and m.sl>0");
            string linker = this.linker.Text.Trim();
            string linker1 = this.linker1.Text.Trim();
            DataTable dt = sacfBLL.GetEnquiriesAll(sb.ToString(), dictionary, linker, linker1); 
            decimal count = 0;
            decimal price1 = 0;
            decimal price2 = 0;
            decimal price3 = 0;
            if (dt != null && dt.Rows.Count > 0)
            {
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    String[] DataRow = new String[33];
                    DataRow[0] = dt.Rows[i]["date"].ToString();
                    DataRow[1] = dt.Rows[i]["no"].ToString();
                    DataRow[2] = dt.Rows[i]["shop_id"].ToString();
                    DataRow[3] = dt.Rows[i]["dep_id"].ToString();
                    DataRow[4] = string.Format("{0} {1}", dt.Rows[i]["id"].ToString(), dt.Rows[i]["goods"].ToString());
                    DataRow[5] = dt.Rows[i]["sl_p"].ToString();
                    DataRow[6] = dt.Rows[i]["pack"].ToString();
                    DataRow[7] = dt.Rows[i]["price_js"].ToString();
                    DataRow[8] = dt.Rows[i]["sl"].ToString();
                    count += decimal.Parse(DataRow[8]);
                    price1 += decimal.Parse(DataRow[8]) * decimal.Parse(DataRow[7]);
                    DataRow[9] = dt.Rows[i]["PRICE_hs"].ToString();
                    DataRow[10] = dt.Rows[i]["JE_hs"].ToString();
                    price2 += decimal.Parse(DataRow[10]);
                    DataRow[11] = dt.Rows[i]["pricei"].ToString();
                    price3 += decimal.Parse(DataRow[8]) * decimal.Parse(DataRow[11]);
                    DataRow[12] = dt.Rows[i]["cus_id"].ToString();
                    DataRow[13] = dt.Rows[i]["cus_shop"].ToString();
                    DataRow[14] = dt.Rows[i]["cus_dep"].ToString();
                    DataRow[15] = dt.Rows[i]["OPER"].ToString();
                    DataRow[16] = dt.Rows[i]["type_js"].ToString();
                    DataRow[17] = dt.Rows[i]["BEI"].ToString();

                    DataRow[18] = string.Format("{0} {1}", dt.Rows[i]["f1"].ToString(), dt.Rows[i]["d_th"].ToString());
                    DataRow[19] = string.Format("{0} {1}", dt.Rows[i]["f2"].ToString(), dt.Rows[i]["d_kp"].ToString());
                    DataRow[20] = string.Format("{0} {1}", dt.Rows[i]["f3"].ToString(), dt.Rows[i]["d_js"].ToString());
                    DataRow[21] = string.Format("{0} {1}", dt.Rows[i]["f4"].ToString(), dt.Rows[i]["d_ok"].ToString());

                    DataRow[22] = dt.Rows[i]["slkc"].ToString();
                    DataRow[23] = dt.Rows[i]["jekc"].ToString();
                    DataRow[24] = dt.Rows[i]["no_cus"].ToString();
                    DataRow[25] = dt.Rows[i]["type1"].ToString();
                    DataRow[26] = dt.Rows[i]["type2"].ToString();
                    DataRow[27] = dt.Rows[i]["type3"].ToString();
                    DataRow[28] = string.Format("{0}{1}{2}{3}", dt.Rows[i]["f1"].ToString(), dt.Rows[i]["f2"].ToString(), dt.Rows[i]["f3"].ToString(), dt.Rows[i]["f4"].ToString());
                    DataRow[29] = dt.Rows[i]["dm"].ToString();
                    DataRow[30] = dt.Rows[i]["f6"].ToString();
                    DataRow[31] = dt.Rows[i]["unit"].ToString();
                    DataRow[32] = dt.Rows[i]["ddno"].ToString();
                    this.dataGridView1.Rows.Add(DataRow);
                }
                this.label17.Text = string.Format("● 共{10}条 合计数量：{0} 金额：{1} 折让：{2} 实现毛利1：{3} 成本：{4} 当时进价：{5} \n结算金额：{6} 实现毛利2：{7} 成本：{8} 当时进价：{9}", (int)count, price2.ToString("0.00"), (price2 - price1).ToString("0.00"), (price2 - price3).ToString("0.00"), price3.ToString("0.00"), price3.ToString("0.00"), price1.ToString("0.00"), (price1 - price3).ToString("0.00"), price3.ToString("0.00"), price3.ToString("0.00"), dt.Rows.Count);
            }
            else
            {
                this.label17.Text = "";
                MessageBox.Show("查无符合条件的记录！", "信息窗", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
        }

        private void txtID_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                if (!string.IsNullOrEmpty(this.txtID.Text.Trim()))
                {
                    MisEntity.Goods goods = goodsBLL.EndsWithID(this.txtID.Text.Trim());
                    if (goods == null)
                    {
                        MessageBox.Show("无此商品,请先设置！", "信息窗", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        this.txtID.Focus();
                        return;
                    }
                    this.txtGoods.Text = goods.GOODS;
                    this.txtDM.Text = goods.DM;
                    this.txtID.Text = goods.ID;
                }
                this.txtDepCode.Focus();
            }
        }

        private void txtCusCode_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                if (!string.IsNullOrEmpty(this.txtCusCode.Text.Trim()))
                {
                    MisModel.Customer customer = cusBLL.GetById(this.txtCusCode.Text.Trim());
                    if (customer == null)
                    {
                        MessageBox.Show("无此代码或不属于本部分！", "信息窗", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        this.txtCusCode.Focus();
                        return;
                    }
                    this.txtBoxCus.Text = customer.Customer_;
                }
                this.txtCusDepCode.Focus();
            }
        }

        private void txtType1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                if (!string.IsNullOrEmpty(this.txtType1.Text.Trim()))
                {
                    MisModel.Type type = typeTjBLL.GetType1ById(this.txtType1.Text.Trim());
                    if (type == null)
                    {
                        MessageBox.Show("统计类型不存在,或无权操作！", "信息窗", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        this.txtType1.Focus();
                        return;
                    }
                    else
                        this.textBox29.Text = type.TypeName;
                }
                this.txtType2.Focus();
            }
        }

        private void txtType2_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                if (!string.IsNullOrEmpty(this.txtType2.Text.Trim()))
                {
                    MisModel.Type type = typeTjBLL.GetType1ById(this.txtType2.Text.Trim());
                    if (type == null)
                    {
                        MessageBox.Show("统计类型不存在,或无权操作！", "信息窗", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        this.txtType2.Focus();
                        return;
                    }
                    else
                        this.textBox31.Text = type.TypeName;
                }
                this.txtType3.Focus();
            }
        }

        private void txtType3_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                if (!string.IsNullOrEmpty(this.txtType3.Text.Trim()))
                {
                    MisModel.Type type = typeTjBLL.GetType1ById(this.txtType3.Text.Trim());
                    if (type == null)
                    {
                        MessageBox.Show("统计类型不存在,或无权操作！", "信息窗", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        this.txtType3.Focus();
                        return;
                    }
                    else
                        this.textBox9.Text = type.TypeName;
                }
                this.btnOk.Focus();
            }
        }

        private void txtGysCode_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                if (!string.IsNullOrEmpty(this.txtGysCode.Text.Trim()))
                {
                    MisModel.Supply supply = supplyBLL.GetById(this.txtGysCode.Text.Trim());
                    if (supply == null)
                    {
                        MessageBox.Show("供应商不存在,或无权操作！", "信息窗", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        this.txtGysCode.Focus();
                        return;
                    }
                    else
                        this.txtGysName.Text = supply.SuppName;
                }
                this.txtCusCode.Focus();
            }  
        }

        private void expireStarDate_ValueChanged(object sender, EventArgs e)
        {
            this.expireStarDate.Format = DateTimePickerFormat.Long;
            this.expireStarDate.CustomFormat = null;
        }

        private void expireEndDate_ValueChanged(object sender, EventArgs e)
        {
            this.expireEndDate.Format = DateTimePickerFormat.Long;
            this.expireEndDate.CustomFormat = null;
            
        }

        private void openStarDate_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                this.openEndDate.Focus();
            }
        }

        private void openEndDate_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                this.expireStarDate.Focus();
            }
            
        }

        private void expireStarDate_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                this.expireEndDate.Focus();
            }
            
        }

        private void expireEndDate_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                this.txtNo.Focus();
            }
            
        }

        private void txtNo_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                this.txtShopCode.Focus();
            }
            
        }

        private void txtShopCode_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                this.txtID.Focus();
            }
            
        }

        private void txtCusDepCode_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                this.txtInvoiceNum.Focus();
            }
            
        }

        private void txtInvoiceNum_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                this.txtOper.Focus();
            }
            
        }

        private void txtOper_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                this.txtTax.Focus();
            }
            
        }

        private void txtTax_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                this.txtCusShopCode.Focus();
            }
            
        }

        private void txtCusShopCode_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                this.comboBox1.Focus();
            }
            
        }

        private void comboBox1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                this.txtFKID.Focus();
            }
            
        }

        private void txtFKID_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                this.comboBox2.Focus();
            }
            
        }

        private void comboBox2_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                this.comboBox3.Focus();
            }
            
        }

        private void comboBox3_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                this.txtType1.Focus();
            }
            
        }

        private void contextMenuStrip1_Opening(object sender, CancelEventArgs e)
        {

        }

        private void toolStripMenuItem_Click(object sender, EventArgs e)
        {
            System.Diagnostics.Process.Start("calc.exe");
        }

        private void btnRef_Click(object sender, EventArgs e)
        {
            Point mousePoint = MousePosition;
            Point point = this.PointToClient(mousePoint);
            this.contextMenuStrip1.Show(this, point);
        }

        private void btnQuit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
        {
            if(1==this.printType)
                webPrint1(e);
            else if(2==this.printType)
                webPrint2(e);
            else if (3 == this.printType)
                webPrint3(e);
            else if (4 == this.printType)
                webPrint4(e);
        }

        private void webPrint1(System.Drawing.Printing.PrintPageEventArgs e)
        {
            string _Title = "新江厦汽车销售公司销售查询表.格式一";
            Font _TitleFont = new Font("宋体", 20, FontStyle.Bold);
            Point _CurrentPoint = new Point(30, 30);
            _CurrentPoint.X = (e.PageBounds.Width) / 2 - (int)(e.Graphics.MeasureString(_Title, _TitleFont).Width) / 2;
            e.Graphics.DrawString(_Title, _TitleFont, new SolidBrush(Color.Black), _CurrentPoint);
            StringBuilder sb = new StringBuilder();
            decimal sp = 0;
            sb.Append(string.Format("打印日期：{0}                部门：{1}\n", this.openStarDate.Value.ToShortDateString(), this.txtDepCode.Text.Trim()));
            sb.Append("━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\n");
            sb.Append(" 日期 库 单据号      内码 商品名称             规格  销售数量  含税金额  客户  客户部  进程\n");
            sb.Append("━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\n");
            if (this.dataGridView1.Rows.Count > 0)
            {
                DataGridViewRow dr = null;
                for (int i = dataGridView; i < this.dataGridView1.RowCount; i++, dataGridView++)
                {
                    dr = this.dataGridView1.Rows[i];
                    sb.Append(Convert.ToDateTime(dr.Cells[0].Value.ToString()).ToString("MM.dd")).Append(" ");
                    sb.Append(dr.Cells[2].Value.ToString()).Append(" ");
                    sb.Append(dr.Cells[1].Value.ToString()).Append(" ");
                    sb.Append(dr.Cells[29].Value.ToString()).Append(" ");
                    sb.Append(winPrint.getGoodsStr(20, dr.Cells[4].Value.ToString().Split(' ')[1]));
                    sb.Append(winPrint.getLenStr(5, dr.Cells[6].Value.ToString()));
                    sb.Append(winPrint.getLenStr(10, decimal.Parse(dr.Cells[8].Value.ToString()).ToString("f2")));
                    sp += decimal.Parse(dr.Cells[8].Value.ToString());
                    sb.Append(winPrint.getLenStr(10, decimal.Parse(dr.Cells[10].Value.ToString()).ToString("f2"))).Append(" ");
                    sb.Append(dr.Cells[12].Value.ToString()).Append(" ");
                    sb.Append(dr.Cells[14].Value.ToString()).Append(" ");
                    sb.Append(dr.Cells[28].Value.ToString()).Append("\n");
                    if (i != 0 && i % 44 == 0)
                    {
                        dataGridView++;
                        break;
                    }
                }
            }
            sb.Append("━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\n");
            sb.Append(string.Format("合计数量：{0}", sp.ToString("f2"))).Append("\n");
            sb.Append("━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\n");
            sb.Append(string.Format("制表：{0} 姓名{1}      日期：{2}", accountUser.UserCode, accountUser.Oper, DateTime.Now));

            _CurrentPoint.X = 30;
            _CurrentPoint.Y += (int)(e.Graphics.MeasureString(_Title, _TitleFont).Height) + 20;
            Font _ContentFont = new Font("宋体", 12);
            e.Graphics.DrawString(sb.ToString(), _ContentFont, new SolidBrush(Color.Black), _CurrentPoint);
            if (dataGridView < this.dataGridView1.RowCount - 1)
                e.HasMorePages = true;
            else
                e.HasMorePages = false;
        }

        private void webPrint2(System.Drawing.Printing.PrintPageEventArgs e)
        {
            string _Title = "新江厦汽车销售公司销售查询表.格式二";
            Font _TitleFont = new Font("宋体", 20, FontStyle.Bold);
            Point _CurrentPoint = new Point(30, 30);
            _CurrentPoint.X = (e.PageBounds.Width) / 2 - (int)(e.Graphics.MeasureString(_Title, _TitleFont).Width) / 2;
            e.Graphics.DrawString(_Title, _TitleFont, new SolidBrush(Color.Black), _CurrentPoint);
            StringBuilder sb = new StringBuilder();
            decimal sp = 0;
            sb.Append(string.Format("打印日期：{0}                部门：{1}\n", this.openStarDate.Value.ToShortDateString(), this.txtDepCode.Text.Trim()));
            sb.Append("━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\n");
            sb.Append("销售日期 库 单据号     客户  商品名称               销售数量  含税金额 进程\n");
            sb.Append("━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\n");
            if (this.dataGridView1.Rows.Count > 0)
            {
                DataGridViewRow dr = null;
                for (int i = dataGridView; i < this.dataGridView1.RowCount; i++, dataGridView++)
                {
                    dr = this.dataGridView1.Rows[i];
                    sb.Append(Convert.ToDateTime(dr.Cells[0].Value.ToString()).ToString("yy.MM.dd")).Append(" ");
                    sb.Append(dr.Cells[2].Value.ToString()).Append(" ");
                    sb.Append(dr.Cells[1].Value.ToString()).Append(" ");
                    sb.Append(dr.Cells[12].Value.ToString()).Append(" ");
                    sb.Append(winPrint.getGoodsStr(20, dr.Cells[4].Value.ToString().Split(' ')[1]));
                    sb.Append(winPrint.getLenStr(11, decimal.Parse(dr.Cells[8].Value.ToString()).ToString("f2")));
                    sp += decimal.Parse(dr.Cells[8].Value.ToString());
                    sb.Append(winPrint.getLenStr(10, decimal.Parse(dr.Cells[10].Value.ToString()).ToString("f2"))).Append(" ");
                    sb.Append(dr.Cells[28].Value.ToString()).Append("\n");
                    if (i != 0 && i % 44 == 0)
                    {
                        dataGridView++;
                        break;
                    }
                }
            }
            sb.Append("━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\n");
            sb.Append(string.Format("合计数量：{0}", sp.ToString("f2"))).Append("\n");
            sb.Append("━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\n");
            sb.Append(string.Format("制表：{0} 姓名{1}      日期：{2}", accountUser.UserCode, accountUser.Oper, DateTime.Now));

            _CurrentPoint.X = 30;
            _CurrentPoint.Y += (int)(e.Graphics.MeasureString(_Title, _TitleFont).Height) + 20;
            Font _ContentFont = new Font("宋体", 12);
            e.Graphics.DrawString(sb.ToString(), _ContentFont, new SolidBrush(Color.Black), _CurrentPoint);
            if (dataGridView < this.dataGridView1.RowCount - 1)
                e.HasMorePages = true;
            else
                e.HasMorePages = false;
        }

        private void webPrint3(System.Drawing.Printing.PrintPageEventArgs e)
        {
            string _Title = "新江厦汽车销售公司销售查询表.格式四";
            Font _TitleFont = new Font("宋体", 20, FontStyle.Bold);
            Point _CurrentPoint = new Point(30, 30);
            _CurrentPoint.X = (e.PageBounds.Width) / 2 - (int)(e.Graphics.MeasureString(_Title, _TitleFont).Width) / 2;
            e.Graphics.DrawString(_Title, _TitleFont, new SolidBrush(Color.Black), _CurrentPoint);
            StringBuilder sb = new StringBuilder();
            decimal sp = 0;
            sb.Append(string.Format("打印日期：{0}                部门：{1}\n", this.openStarDate.Value.ToShortDateString(), this.txtDepCode.Text.Trim()));
            sb.Append("━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\n");
            sb.Append(" 日期 库 单据号     商品                            单价    数量  含税金额 客户 进程\n");
            sb.Append("━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\n");
            if (this.dataGridView1.Rows.Count > 0)
            {
                DataGridViewRow dr = null;
                for (int i = dataGridView; i < this.dataGridView1.RowCount; i++, dataGridView++)
                {
                    dr = this.dataGridView1.Rows[i];
                    sb.Append(Convert.ToDateTime(dr.Cells[0].Value.ToString()).ToString("MM.dd")).Append(" ");
                    sb.Append(dr.Cells[2].Value.ToString()).Append(" ");
                    sb.Append(dr.Cells[1].Value.ToString()).Append(" ");
                    sb.Append(string.Format("{0} {1}", dr.Cells[4].Value.ToString().Split(' ')[0], winPrint.getGoodsStr(12, dr.Cells[4].Value.ToString().Split(' ')[1])));
                    sb.Append(winPrint.getLenStr(10, decimal.Parse(dr.Cells[9].Value.ToString()).ToString("f2")));
                    sb.Append(winPrint.getLenStr(8, decimal.Parse(dr.Cells[8].Value.ToString()).ToString("f2")));
                    sp += decimal.Parse(dr.Cells[8].Value.ToString());
                    sb.Append(winPrint.getLenStr(10, decimal.Parse(dr.Cells[10].Value.ToString()).ToString("f2"))).Append(" ");
                    sb.Append(dr.Cells[12].Value.ToString()).Append(" ");
                    sb.Append(dr.Cells[28].Value.ToString()).Append("\n");
                    if (i != 0 && i % 44 == 0)
                    {
                        dataGridView++;
                        break;
                    }
                }
            }
            sb.Append("━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\n");
            sb.Append(string.Format("合计数量：{0}", sp.ToString("f2"))).Append("\n");
            sb.Append("━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\n");
            sb.Append(string.Format("制表：{0} 姓名{1}      日期：{2}", accountUser.UserCode, accountUser.Oper, DateTime.Now));

            _CurrentPoint.X = 30;
            _CurrentPoint.Y += (int)(e.Graphics.MeasureString(_Title, _TitleFont).Height) + 20;
            Font _ContentFont = new Font("宋体", 12);
            e.Graphics.DrawString(sb.ToString(), _ContentFont, new SolidBrush(Color.Black), _CurrentPoint);
            if (dataGridView < this.dataGridView1.RowCount - 1)
                e.HasMorePages = true;
            else
                e.HasMorePages = false;
        }

        private void webPrint4(System.Drawing.Printing.PrintPageEventArgs e)
        {
            Dictionary<string, object> dictionary = new Dictionary<string, object>();
            StringBuilder sb = new StringBuilder();
            this.dataGridView1.Rows.Clear();

            if (!"1".Equals(this.txtDepCode.Text.Trim()))
            {
                sb.Append(" and m.dep_id=@depId");
                dictionary.Add("@depId", this.txtDepCode.Text.Trim());
            }
            sb.Append(" and m.date>=@startDate and m.date<=@endDate");
            dictionary.Add("@startDate", this.openStarDate.Value.ToShortDateString());
            DateTime time = DateTime.Parse(this.openEndDate.Value.ToShortDateString());
            time = time.Date.AddDays(1);
            dictionary.Add("@endDate", time.ToShortDateString());
            if (!string.IsNullOrEmpty(this.expireStarDate.Text.ToString().Trim()))
            {
                sb.Append(" and m.d_kp>=@gqStartDate");
                dictionary.Add("@gqStartDate", this.expireStarDate.Value.ToShortDateString());
            }
            if (!string.IsNullOrEmpty(this.expireEndDate.Text.ToString().Trim()))
            {
                sb.Append(" and m.d_kp<=@gqEndDate");
                time = DateTime.Parse(this.expireEndDate.Value.ToShortDateString());
                time = time.Date.AddDays(1);
                dictionary.Add("@gqEndDate", time.ToShortDateString());
            }
            if (!string.IsNullOrEmpty(this.txtID.Text.Trim()))
            {
                sb.Append(" and m.id=@id");
                dictionary.Add("@id", this.txtID.Text.Trim());
            }
            if (!string.IsNullOrEmpty(this.txtShopCode.Text.Trim()))
            {
                sb.Append(" and m.shop_id=@shopId");
                dictionary.Add("@shopId", this.txtShopCode.Text.Trim());
            }
            if (!string.IsNullOrEmpty(this.txtNo.Text.Trim()))
            {
                sb.Append(" and m.no=@no");
                dictionary.Add("@no", this.txtNo.Text.Trim());
            }
            if (!string.IsNullOrEmpty(this.txtCusCode.Text.Trim()))
            {
                sb.Append(" and m.cus_id=@cusId");
                dictionary.Add("@cusId", this.txtCusCode.Text.Trim());
            }
            if (!string.IsNullOrEmpty(this.txtGysCode.Text.Trim()))
            {
                sb.Append(" and g.supp_id=@suppId");
                dictionary.Add("@suppId", this.txtGysCode.Text.Trim());
            }
            if (!string.IsNullOrEmpty(this.txtCusDepCode.Text.Trim()))
            {
                sb.Append(" and m.cus_dep=@cusDep");
                dictionary.Add("@cusDep", this.txtCusDepCode.Text.Trim());
            }
            if (!string.IsNullOrEmpty(this.txtOper.Text.Trim()))
            {
                sb.Append(" and m.oper=@oper");
                dictionary.Add("@oper", this.txtOper.Text.Trim());
            }
            if (!string.IsNullOrEmpty(this.txtTax.Text.Trim()))
            {
                sb.Append(" and m.tax=@tax");
                dictionary.Add("@tax", this.txtTax.Text.Trim());
            }
            if (!string.IsNullOrEmpty(this.txtCusShopCode.Text.Trim()))
            {
                sb.Append(" and m.cus_shop=@cusShop");
                dictionary.Add("@cusShop", this.txtCusShopCode.Text.Trim());
            }
            if (!string.IsNullOrEmpty(this.txtBei.Text.Trim()))
            {
                sb.Append(" and m.bei=@bei");
                dictionary.Add("@bei", this.txtBei.Text.Trim());
            }
            if (!string.IsNullOrEmpty(this.txtInvoiceNum.Text.Trim()))
            {
                sb.Append(" and m.no_cus=@noCus");
                dictionary.Add("@noCus", this.txtInvoiceNum.Text.Trim());
            }
            if (!string.IsNullOrEmpty(this.txtFKID.Text.Trim()))
            {
                sb.Append(" and m.type_js=@typeJs");
                dictionary.Add("@typeJs", this.txtFKID.Text.Trim());
            }
            if (!string.IsNullOrEmpty(this.txtType1.Text.Trim()))
            {
                sb.Append(" and g.type1=@type1");
                dictionary.Add("@type1", this.txtType1.Text.Trim());
            }
            if (!string.IsNullOrEmpty(this.txtType2.Text.Trim()))
            {
                sb.Append(" and g.type2=@type2");
                dictionary.Add("@type2", this.txtType2.Text.Trim());
            }
            if (!string.IsNullOrEmpty(this.txtType3.Text.Trim()))
            {
                sb.Append(" and g.type3=@type3");
                dictionary.Add("@type3", this.txtType3.Text.Trim());
            }
            string tjStr = this.comboBox1.SelectedItem.ToString();
            if ("已开单".Equals(tjStr))
                sb.Append(" and m.f1='1'");
            else if ("已开单未确认提货".Equals(tjStr))
                sb.Append(" and m.f1='1' and m.f2=''");
            else if ("已确认未提货".Equals(tjStr))
                sb.Append(" and m.f2='0'");
            else if ("未提货,已开发票".Equals(tjStr))
                sb.Append(" and m.f2!='1' and m.f3='1'");
            else if ("已确认提货".Equals(tjStr))
                sb.Append(" and m.f2='1'");
            else if ("已提货,已开发票".Equals(tjStr))
                sb.Append(" and m.f2='1' and m.f3='1'");
            else if ("已提货,未开发票".Equals(tjStr))
                sb.Append(" and m.f2='1' and m.f3!='1'");
            else if ("已开发票".Equals(tjStr))
                sb.Append(" and m.f3='1'");
            else if ("已开发票,未到款".Equals(tjStr))
                sb.Append(" and m.f3='1' and m.f4!='1'");
            else if ("已开发票,已到款".Equals(tjStr))
                sb.Append(" and m.f3='1' and m.f4='1'");
            else if ("已结束".Equals(tjStr))
                sb.Append(" and m.f8='1'");
            tjStr = this.comboBox2.SelectedItem.ToString();
            if ("普通特价".Equals(tjStr))
                sb.Append(" and m.f6='1'");
            else if ("发票高开".Equals(tjStr))
                sb.Append(" and m.f6='2'");
            else if ("非特价".Equals(tjStr))
                sb.Append(" and m.f6=''");
            tjStr = this.comboBox3.SelectedItem.ToString();
            if ("小于0(退货)".Equals(tjStr))
                sb.Append(" and m.sl<0");
            else if ("大于0".Equals(tjStr))
                sb.Append(" and m.sl>0");
            string linker = this.linker.Text.Trim();
            string linker1 = this.linker1.Text.Trim();
            DataTable dt = sacfBLL.GetEnquiriesStatisticsAll(sb.ToString(), dictionary, linker, linker1);
            string _Title = "新江厦副食品公司销售查询表.单据汇总";
            Font _TitleFont = new Font("宋体", 20, FontStyle.Bold);
            Point _CurrentPoint = new Point(30, 30);
            _CurrentPoint.X = (e.PageBounds.Width) / 2 - (int)(e.Graphics.MeasureString(_Title, _TitleFont).Width) / 2;
            e.Graphics.DrawString(_Title, _TitleFont, new SolidBrush(Color.Black), _CurrentPoint);
            sb = new StringBuilder();
            sb.Append(string.Format("打印日期：{0}                部门：{1}\n", this.openStarDate.Value.ToShortDateString(), this.txtDepCode.Text.Trim()));
            sb.Append("━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\n");
            sb.Append(" 日期        单据号        库   部门    含税价     含税额       成本      库存金额\n");
            sb.Append("━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\n");
            if (dt != null && dt.Rows.Count > 0)
            {
                for (int i = dataGridView; i < dt.Rows.Count; i++, dataGridView++)
                {
                    sb.Append(Convert.ToDateTime(dt.Rows[i]["date"].ToString()).ToString("MM.dd")).Append("      ");
                    sb.Append(dt.Rows[i]["no"].ToString()).Append("       ");
                    sb.Append(dt.Rows[i]["shop_id"].ToString()).Append("   ");
                    sb.Append(dt.Rows[i]["dep_id"].ToString()).Append("  ");
                    sb.Append(winPrint.getLenStr(10, decimal.Parse(dt.Rows[i]["price_js"].ToString()).ToString("f2"))).Append(" ");
                    sb.Append(winPrint.getLenStr(10, decimal.Parse(dt.Rows[i]["PRICE_hs"].ToString()).ToString("f2"))).Append("   ");
                    sb.Append(winPrint.getLenStr(10, decimal.Parse(dt.Rows[i]["JE_hs"].ToString()).ToString("f2"))).Append(" ");
                    sb.Append(winPrint.getLenStr(10, decimal.Parse(dt.Rows[i]["pricei"].ToString()).ToString("f2"))).Append("\n"); ;

                    if (i != 0 && i % 44 == 0)
                    {
                        dataGridView++;
                        break;
                    }

                }
                sb.Append("━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\n");
                sb.Append(string.Format("制表：{0}          日期：{1}", accountUser.UserCode, DateTime.Now));

                _CurrentPoint.X = 30;
                _CurrentPoint.Y += (int)(e.Graphics.MeasureString(_Title, _TitleFont).Height) + 20;
                Font _ContentFont = new Font("宋体", 12);
                e.Graphics.DrawString(sb.ToString(), _ContentFont, new SolidBrush(Color.Black), _CurrentPoint);
                if (dataGridView < dt.Rows.Count - 1)
                    e.HasMorePages = true;
                else
                    e.HasMorePages = false;
            }
        }

        private void btnPri1_Click(object sender, EventArgs e)
        {
            this.dataGridView = 0;
            this.printType = 1;
            this.printPreviewDialog1.Document = this.printDocument1;
            this.printPreviewDialog1.PrintPreviewControl.Zoom = 1.0;
            this.printPreviewDialog1.ShowDialog();
        }

        private void btnPri2_Click(object sender, EventArgs e)
        {
            this.dataGridView = 0;
            this.printType = 2;
            this.printPreviewDialog1.Document = this.printDocument1;
            this.printPreviewDialog1.PrintPreviewControl.Zoom = 1.0;
            this.printPreviewDialog1.ShowDialog();
        }

        private void btnPri3_Click(object sender, EventArgs e)
        {
            this.dataGridView = 0;
            this.printType = 1;
            this.printPreviewDialog1.Document = this.printDocument1;
            this.printPreviewDialog1.PrintPreviewControl.Zoom = 1.0;
            this.printPreviewDialog1.ShowDialog();
        }

        private void btnPri4_Click(object sender, EventArgs e)
        {
            this.dataGridView = 0;
            this.printType = 3;
            this.printPreviewDialog1.Document = this.printDocument1;
            this.printPreviewDialog1.PrintPreviewControl.Zoom = 1.0;
            this.printPreviewDialog1.ShowDialog();
        }

        private void btnZoom_Click(object sender, EventArgs e)
        {
            if (this.dataGridView1.Location.Y == 10)
            {
                this.dataGridView1.Location = new Point(this.dataGridView1.Location.X, 250);
                this.dataGridView1.Size = new Size(this.dataGridView1.Width, this.Height - 400);
            }
            else
            {
                this.dataGridView1.Location = new Point(this.dataGridView1.Location.X, 10);
                this.dataGridView1.Size = new Size(this.dataGridView1.Width, this.Height - 160);
            }
        }

        private void btnTogetherPrint_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(this.txtDepCode.Text.Trim()))
            {
                MessageBox.Show("部门代码不能为空！", "信息窗", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                this.txtDepCode.Focus();
                return;
            }
            MisModel.Depart depart = departBLL.GetById(this.txtDepCode.Text.Trim());
            if (depart == null)
            {
                MessageBox.Show("此部门无权操作！", "信息窗", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                this.txtDepCode.Focus();
                return;
            }

            this.dataGridView = 0;
            this.printType = 4;
            this.printPreviewDialog1.Document = this.printDocument1;
            this.printPreviewDialog1.PrintPreviewControl.Zoom = 1.0;
            this.printPreviewDialog1.ShowDialog();
        }
    }
}
